package com.ecoolex.ying.space.api.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.entity.UserFavorite;
import com.ecoolex.ying.space.entity.UserFavoriteItems;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.pojo.AddUserFavoriteItemsDTO;
import com.ecoolex.ying.space.ws.UserFavoriteItemsService;
import com.ecoolex.ying.space.ws.UserFavoriteService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;


@GraphQL2Module(type = GraphQL2Module.Type.User, description = "用户收藏")
public class UserFavoriteItemsGraphql {

    @Reference
    UserFavoriteItemsService userFavoriteItemsService;

    @Reference
    UserFavoriteService userFavoriteService;

    @TypeField(type = UserFavoriteItems.class, field = "Content", description = "收藏作品内容")
    public UserWorks works(@Source UserFavoriteItems source) {
        return userFavoriteItemsService.getUserWorks(source.getContentId());
    }

    @TypeField(type = UserFavoriteItems.class, field = "ContentAuthor", description = "收藏作品内容的作者")
    public ConsumerMember contentAuthor(@Source UserFavoriteItems source) {
        Integer userId = source.getContentAuthorUserId();
        return userFavoriteItemsService.getContentAuthor(userId);
    }

    @MutationField(field = "addUserFavoriteItems", description = "收藏内容")
    public UserFavoriteItems addUserFavoriteItems(@NonNull AddUserFavoriteItemsDTO add, ConsumerMemberAuthority user) {
        user.assertLogin();
        String createUser = user.userName();
        UserFavoriteItems record = add;
        record.setUserId(user.userId());
        return userFavoriteItemsService.addUserFavoriteItems(record, createUser);
    }

    @MutationField(field = "cancleUserFavoriteItems", description = "取消收藏内容")
    public boolean deleteUserFavoriteItems(@NonNull Integer favoriteItemId, ConsumerMemberAuthority user) {
        user.assertLogin();
        String updateUser = user.userName();
        UserFavoriteItems record
            = userFavoriteItemsService.getUserFavoriteItemsByUserIdAndFavoriteItemId(user.userId(), favoriteItemId);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "收藏的纪录不存在");
        userFavoriteItemsService.deleteUserFavoriteItems(record, updateUser);
        return true;
    }

    @QueryField(field = "userFavoriteItemsListByFavoriteId", description = "根据收藏夹id查询收藏内容列表")
    public Pagination<UserFavoriteItems> queryFavoriteItemsListByFavoriteId(@NonNull Integer favoriteId, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserFavorite record = userFavoriteService.getUserFavoriteByIdAndUserId(user.userId(), favoriteId);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "收藏夹不存在");
        return userFavoriteItemsService.queryUserFavoriteItemsByFavoriteId(favoriteId, page);
    }

    @QueryField(field = "myUserFavoriteItemsList", description = "个人主页-自己的收藏内容列表")
    public Pagination<UserFavoriteItems> queryMyFavoriteItemsList(@NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userFavoriteItemsService.queryMyUserFavoriteItems(user.userId(), page);
    }

    @QueryField(field = "myUserFavoriteItemsCount", description = "个人主页-自己的收藏数量")
    public long queryMyFavoriteItemsCount(ConsumerMemberAuthority user) {
        user.assertLogin();
        return userFavoriteItemsService.queryMyUserFavoriteItemsCount(user.userId());
    }

    @QueryField(field = "otherUserFavoriteItemsList", description = "个人主页-他人的收藏内容列表")
    public Pagination<UserFavoriteItems> queryOtherFavoriteItemsList(@NonNull Integer userId, @NonNull Page page) {
        return userFavoriteItemsService.queryOtherUserFavoriteItems(userId, page);
    }

    @QueryField(field = "otherUserFavoriteItemsCount", description = "个人主页-他人的收藏数量")
    public long queryOtherFavoriteItemsCount(@NonNull Integer userId) {
        return userFavoriteItemsService.queryOtherUserFavoriteItemsCount(userId);
    }
}
